# !/usr/bin/env python
# -*- coding:utf-8 -*-
# project name: bitu-Interface-test
# author: "Lei Yong" 
# creation time: 2019/7/31 15:14
# Email: leiyong711@163.com

import json
import requests
from config import setting


# 重写requests方法
class Request:
    def __init__(self):
        self.base_url = setting.HOST
        self.time_out = setting.TIME_OUT
        self.headers = {}

    def Get(self, url, header):

        if header:
            print(f"headers： {header}")
            # 合并全局变量与用例中的headers字典
            self.headers.update(json.loads(header))
            self.headers.update(setting.HEADERS)

        return requests.get(url=self.base_url + url, headers=self.headers, timeout=self.time_out)

    def Post(self, url, data, header):

        if header:
            print(f"headers： {header}")
            # 合并全局变量与用例中的headers字典
            self.headers.update(json.loads(header))
            self.headers.update(setting.HEADERS)

        if not data:
            resp = requests.post(url=self.base_url + url, headers=self.headers, timeout=self.time_out)
        else:
            resp = requests.post(url=self.base_url + url, json=json.loads(data), headers=self.headers, timeout=self.time_out)
        return resp
